package com.mall.shopping.services;

import com.mall.shopping.IProductCateService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ProductCateConverter;
import com.mall.shopping.dal.entitys.ItemCat;
import com.mall.shopping.dal.persistence.ItemCatMapper;
import com.mall.shopping.dto.AllProductCateRequest;
import com.mall.shopping.dto.AllProductCateResponse;
import com.mall.shopping.dto.ProductCateDto;
import org.apache.dubbo.config.annotation.Service;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
@Service
public class ICategoriesServiceImpl implements IProductCateService {
    @Autowired
    ItemCatMapper itemCatMapper;

    @Autowired
    ProductCateConverter productCateConverter;

    @Override
    public AllProductCateResponse getAllProductCate(AllProductCateRequest request) {
        AllProductCateResponse allProductCateResponse = new AllProductCateResponse();
        ArrayList<ProductCateDto> productCateDtoList = new ArrayList<>();
        try {
            request.requestCheck();
            Example example = new Example(ItemCat.class);
            example.setOrderByClause(request.getSort());
            List<ItemCat> itemCatList = itemCatMapper.selectByExample(example);
            for (ItemCat itemCat : itemCatList) {
                //遍历将每条数据都进行转换
                ProductCateDto productCateDto = productCateConverter.item2Dto(itemCat);
                productCateDtoList.add(productCateDto);
            }
            allProductCateResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            allProductCateResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            allProductCateResponse.setProductCateDtoList(productCateDtoList);
        } catch (Exception e) {
            e.printStackTrace();
            allProductCateResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            allProductCateResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return allProductCateResponse;
    }
}
